import requests

import requests

headers = {
    'accept': 'application/json, text/plain, */*',
    'accept-language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',
    'priority': 'u=1, i',
    'referer': 'https://www.douyin.com/user/MS4wLjABAAAAd0DN9q5BiMJzPrFdbzjaczfFOiA8SKWoHh0tnCw5MiA?from_tab_name=main',
    'sec-ch-ua': '"Chromium";v="142", "Google Chrome";v="142", "Not_A Brand";v="99"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'sec-fetch-dest': 'empty',
    'sec-fetch-mode': 'cors',
    'sec-fetch-site': 'same-origin',
    'uifid': '3336119d6ecc0f2721002588cf880fbc4753b1582d4c54bc5f80aa9273463f02ae1d7f9ee5d38227ff61d8dca52dab065fe6e0c1937b105b86d1da9ddce6bdba72986ae5dd9cf24d8dc5b069035fcc5215c9b9f2b0ea88e2c0d45ac260553f5df29b1151552603add525df1cb55bb4dba99e06164662b37d417d6fd8ae31e5704db7d002e392aab36092b7a73f7964987f3907cf74dc6cafde80dda9ad394ed882c31e0a3e87ae97f33e599c29cbe9568b011e43bf0bed3a92c255082b7ddefc',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36',
    'cookie': '__ac_nonce=069218f3c0063d008e189; __ac_signature=_02B4Z6wo00f016HnkXAAAIDALE8o-HBQeI-hx5XAAIFg96; enter_pc_once=1; UIFID_TEMP=3336119d6ecc0f2721002588cf880fbc4753b1582d4c54bc5f80aa9273463f02ae1d7f9ee5d38227ff61d8dca52dab065fe6e0c1937b105b86d1da9ddce6bdba900aed0ae22e52cdb78ebd5fcc3a0446d751b155464b167dc84acbf6f71792e9c05ced59ac91e919a628e06180fc7df0; x-web-secsdk-uid=4c3d8e18-0a9f-4914-b260-f35549f70be5; s_v_web_id=verify_mia564tb_qBEYk2Tk_IsBF_4osF_8g6c_l68kDrK0tYWI; _qimei_uuid42=19b1612173a100a661dfc5380b523373e09554aa7f; _qimei_fingerprint=a229050914a38b1064b6f8f37aeeb75e; _qimei_i_3=71c46bd6930903dc90c6aa615cd175e6ffeca0f91a520480bd8d2b5f7495246f326661943989e2a2b4bf; _qimei_h38=4656ad0961dfc5380b52337302000004b19b16; douyin.com; device_web_cpu_core=20; device_web_memory_size=8; architecture=amd64; dy_swidth=1680; dy_sheight=1120; stream_recommend_feed_params=%22%7B%5C%22cookie_enabled%5C%22%3Atrue%2C%5C%22screen_width%5C%22%3A1680%2C%5C%22screen_height%5C%22%3A1120%2C%5C%22browser_online%5C%22%3Atrue%2C%5C%22cpu_core_num%5C%22%3A20%2C%5C%22device_memory%5C%22%3A8%2C%5C%22downlink%5C%22%3A10%2C%5C%22effective_type%5C%22%3A%5C%224g%5C%22%2C%5C%22round_trip_time%5C%22%3A50%7D%22; hevc_supported=true; strategyABtestKey=%221763807039.918%22; xgplayer_user_id=856399400001; passport_csrf_token=e2eac26db08424f81cb91c20b13ffcbc; passport_csrf_token_default=e2eac26db08424f81cb91c20b13ffcbc; fpk1=U2FsdGVkX19F6GEYpttAkNOA5YGUtc5crYgOaifm+MGgVoLc2Q/wElpl3kHtZiYCCg4ZWum8QVSqr284JUSSBg==; fpk2=3c9fc7ddec9b58823c1c96756dbd45d8; __security_mc_1_s_sdk_crypt_sdk=59bfd63d-4611-a962; bd_ticket_guard_client_web_domain=2; sdk_source_info=7e276470716a68645a606960273f276364697660272927676c715a6d6069756077273f276364697660272927666d776a68605a607d71606b766c6a6b5a7666776c7571273f275e58272927666a6b766a69605a696c6061273f27636469766027292762696a6764695a7364776c6467696076273f275e582729277672715a646971273f2763646976602729277f6b5a666475273f2763646976602729276d6a6e5a6b6a716c273f2763646976602729276c6b6f5a7f6367273f27636469766027292771273f2734303234313532353d36333234272927676c715a75776a716a666a69273f2763646976602778; bit_env=5Gm4NYz8kJ94Ao-cFEhqYHifBsDfKoKsXH9fDEV9dj0CQPQY5NVJYM2mV6pgBzAElkcqSmytq3cadfUpG3tC7VzivnPVBwER-776fj6qGgp3Jb33kN0ZaGz7FBA5zizQnttBmxGMbT1eeiR7S1_JWN4LicyWdvpFJaJzicDXdJDChVYNoStcw8iAT53gK9D3oEkvpARymp6q_nErG291H1mhQwhfzEFCLm2k1kck7wIxB6185sK85Zc7E-yieSqfodmfpsgOr3w3qRmuO1EAXk-7p5u33aQiNEBd0U0F6G278l2ntqehkdiKrAIrMUrZT_DHveTRnPIRFCBu1YHrAmyZ7Q7MAjD5DFOjKspmsqkLTCzsGDdwgP3KH1vius_HE-pgkTPfcyNO7cVlOne5Jq660Yo0ipM_tSl1k6GAYkbevGoGdp8YWZftg2HPZPQ4K0iUP1OL1XUz5cN7F3jlAxo5OMVsOnbmD4k0JEQCfXOLpWkPZZHTfvnX6UwWq8gC; gulu_source_res=eyJwX2luIjoiZDg5ZGRhM2JhYWRjMGY2Yzk3MDk4ZWM2Nzk1MTFlZjJjMGMwZTFmMDUyZTBlMTI5MzRkNDg2YzhlM2JjYmZkOSJ9; passport_auth_mix_state=vckwi72kactai0o5ydx7uvlusr8m3isl; odin_tt=2fbcc69a7c1174a99812c97dfc253e1bb7ec29f5d7c8d480108380e298d0f53b931b4de017a85361478940548c460c0ca91482085737f1f3da4fb30db2c28d809530c2d49939de1e862d90f203e3ae06; xg_device_score=7.639736133628393; volume_info=%7B%22isMute%22%3Afalse%2C%22isUserMute%22%3Atrue%2C%22volume%22%3A0.5%7D; is_dash_user=1; stream_player_status_params=%22%7B%5C%22is_auto_play%5C%22%3A0%2C%5C%22is_full_screen%5C%22%3A0%2C%5C%22is_full_webscreen%5C%22%3A0%2C%5C%22is_mute%5C%22%3A0%2C%5C%22is_speed%5C%22%3A1%2C%5C%22is_visible%5C%22%3A0%7D%22; UIFID=3336119d6ecc0f2721002588cf880fbc4753b1582d4c54bc5f80aa9273463f02ae1d7f9ee5d38227ff61d8dca52dab065fe6e0c1937b105b86d1da9ddce6bdba72986ae5dd9cf24d8dc5b069035fcc5215c9b9f2b0ea88e2c0d45ac260553f5df29b1151552603add525df1cb55bb4dba99e06164662b37d417d6fd8ae31e5704db7d002e392aab36092b7a73f7964987f3907cf74dc6cafde80dda9ad394ed882c31e0a3e87ae97f33e599c29cbe9568b011e43bf0bed3a92c255082b7ddefc; _qimei_i_1=64dc2b86935f568a9597f7365ad025b3f2edacf1405e0181b7882b582493206c616333c73980b0ddd0b2fddb; bd_ticket_guard_client_data=eyJiZC10aWNrZXQtZ3VhcmQtdmVyc2lvbiI6MiwiYmQtdGlja2V0LWd1YXJkLWl0ZXJhdGlvbi12ZXJzaW9uIjoxLCJiZC10aWNrZXQtZ3VhcmQtcmVlLXB1YmxpYy1rZXkiOiJCT1pZWUFwU2V5TXpRaFpUeWtUcW5GS3lBTFhOeHdXTDZqS1c3U25VWTV5a1ozUXhCNUFpWWZpM0t2WWVTZHIrVmlwUmJJS0E4c1c4aGdFT2RXNm5lM2s9IiwiYmQtdGlja2V0LWd1YXJkLXdlYi12ZXJzaW9uIjoyfQ%3D%3D; bd_ticket_guard_client_data_v2=eyJyZWVfcHVibGljX2tleSI6IkJPWllZQXBTZXlNelFoWlR5a1RxbkZLeUFMWE54d1dMNmpLVzdTblVZNXlrWjNReEI1QWlZZmkzS3ZZZVNkcitWaXBSYklLQThzVzhoZ0VPZFc2bmUzaz0iLCJyZXFfY29udGVudCI6InNlY190cyIsInJlcV9zaWduIjoibWcrMkFEYW9SUWlWNVJpd1Nuck5WdWk4Z29KVlAxUEs4MkZlekQrZVEyTT0iLCJzZWNfdHMiOiIjcjhONXNTNmVocGFtTzJORi9tZEpKWFYzRlN6ZzVzOHloeE1YM3ZqUXhhdkFUL3RwYW0zRzdNNStXZHp2In0%3D; ttwid=1%7CEeqfxiGi4sdxfizoc0ZFlux2VoXJDW5uhq6yE6SPwNY%7C1763807050%7Cfcdbfbf45e45cf8520caee484b96d27f495fe552f7d6eb09d765b6377ba1e551; biz_trace_id=9dced2dc; IsDouyinActive=true; home_can_add_dy_2_desktop=%220%22',
}

params = {
    'device_platform': 'webapp',
    'aid': '6383',
    'channel': 'channel_pc_web',
    'sec_user_id': 'MS4wLjABAAAAd0DN9q5BiMJzPrFdbzjaczfFOiA8SKWoHh0tnCw5MiA',
    'max_cursor': '0',
    'locate_query': 'false',
    'show_live_replay_strategy': '1',
    'need_time_list': '1',
    'time_list_query': '0',
    'whale_cut_token': '',
    'cut_version': '1',
    'count': '18',
    'publish_video_strategy_type': '2',
    'from_user_page': '1',
    'update_version_code': '170400',
    'pc_client_type': '1',
    'pc_libra_divert': 'Windows',
    'support_h265': '1',
    'support_dash': '1',
    'cpu_core_num': '20',
    'version_code': '290100',
    'version_name': '29.1.0',
    'cookie_enabled': 'true',
    'screen_width': '1680',
    'screen_height': '1120',
    'browser_language': 'zh-CN',
    'browser_platform': 'Win32',
    'browser_name': 'Chrome',
    'browser_version': '142.0.0.0',
    'browser_online': 'true',
    'engine_name': 'Blink',
    'engine_version': '142.0.0.0',
    'os_name': 'Windows',
    'os_version': '10',
    'device_memory': '8',
    'platform': 'PC',
    'downlink': '10',
    'effective_type': '4g',
    'round_trip_time': '50',
    'webid': '7575493504513541632',
    'uifid': '3336119d6ecc0f2721002588cf880fbc4753b1582d4c54bc5f80aa9273463f02ae1d7f9ee5d38227ff61d8dca52dab065fe6e0c1937b105b86d1da9ddce6bdba72986ae5dd9cf24d8dc5b069035fcc5215c9b9f2b0ea88e2c0d45ac260553f5df29b1151552603add525df1cb55bb4dba99e06164662b37d417d6fd8ae31e5704db7d002e392aab36092b7a73f7964987f3907cf74dc6cafde80dda9ad394ed882c31e0a3e87ae97f33e599c29cbe9568b011e43bf0bed3a92c255082b7ddefc',
    'verifyFp': 'verify_mia564tb_qBEYk2Tk_IsBF_4osF_8g6c_l68kDrK0tYWI',
    'fp': 'verify_mia564tb_qBEYk2Tk_IsBF_4osF_8g6c_l68kDrK0tYWI',
    'msToken': 'y3dT_KbNg34NGrozYQTxb4REQUhCUgnVO3g5xdRpMWRuknjheZBx1wujX8aGzWBUnhsbYsKlDc9oxQhZEUBamt3v0bpWdvtlfxXic92fiTauHwOFTYFUAqK7O9bu_hjK8RUT4Ho14LTszZQ5BVKz7Co_tRm5hKfiy-s=',
    'a_bogus': 'Qf0Vg7yEQNWcOVKSYcrpHn/UbzLlNB8y2BTdSiIPexTTOwzahuNQMxtyboFpsk5bf8piho179fU/Yxnc0GX0ZCrpqmpDShG69s5V9U6o2qivTeUMDqbEewszwwMzMb4L-554il862Urq1dO6ird8/p-ntKoCQbfZzrOWk/zaEITg1M6ALZclPpb2EhiqCE==',
    'x-secsdk-web-expire': '1763810660007',
    'x-secsdk-web-signature': 'e3dcfad8ebaf1b5bcfcc58e7ebdc2265',
}


response = requests.get('https://www.douyin.com/aweme/v1/web/aweme/post/', params=params, headers=headers)

aweme_list = response.json().get('aweme_list')

print(aweme_list)

play_addr_url_list = [(aweme.get("desc"),aweme.get("video").get("play_addr").get("url_list")[1]) for aweme in aweme_list]

print(play_addr_url_list)

for title, play_addr_url in play_addr_url_list:
    response = requests.get(play_addr_url)
    #print(response.content)
    title= title.replace("\n", "")
    with open(f"./小苏菲/{title}.avi", "wb") as f:
        f.write(response.content)

    print(f"{title}.mp4下载成功！")